package com.xwd.service.impl;

import com.xwd.dao.UsersDao;
import com.xwd.dao.impl.MiddleDaoImpl;
import com.xwd.dao.impl.UsersDaoImpl;
import com.xwd.domain.Menu;
import com.xwd.domain.Role;
import com.xwd.domain.Users;
import com.xwd.service.RoleService;
import com.xwd.service.UsersService;

import java.util.ArrayList;
import java.util.List;

public class UsersServiceImpl  implements UsersService {

    private UsersDao dao= new UsersDaoImpl();

    @Override
    public Users login(String username,String password) {
        Users user = dao.login(username, password);
        if(user==null){
            return null;
        }
        Integer roleId = user.getRoleId();
        RoleService roleService = new RoleServiceImpl();
        Role role = roleService.getRoleByid(roleId);
        List<Menu> menuList = new MenuServiceImpl().getMenuListByRoleId(roleId);
        List<Menu> newMenuList = new ArrayList<>();
        for (Menu menu : menuList) {
            if(menu.getUpmenuId()==0){
                List<Menu> secondMenuList = new ArrayList<>();
                for (Menu menu1 : menuList) {
                    if(menu1.getUpmenuId()==menu.getMenuId()){
                        secondMenuList.add(menu1);
                    }
                }
                menu.setSecondMenu(secondMenuList);
                newMenuList.add(menu);
            }
        }
        role.setMenu(newMenuList);
        user.setRole(role);
        return user;
    }

    @Override
    public List<Users> getUserlist(int pageIndex, int pageSize) {
        return dao.getUserlist(pageIndex,pageSize);
    }

    @Override
    public int getTotle() {
        return dao.getTotle();
    }

    @Override
    public int addUses(Users users) {
        return dao.addUses(users);
    }
}
